<?php
/**
 * 订单系统  属于半独立系统，权限方面不考虑
 * 2013-2-27
 * @author wolf Email: 116311316@qq.com
 *
 */
class OrderController extends NodeController {
	//以下三个参数对应的是积分记录 而不是订单
	private $chargeType = array ("充值" => 0, "消费" => 1, "赠送" => 2 );
	private $payment = array ("收入" => 0, "支出" => 1 );
	private $status = array ("全部" => "0", "等待审核" => 1, "成功" => 8, "关闭" => - 1 );
	private $_pageNum = 20; //分页订单显示条数
	

	/**
	 * 订单查询
	 */
	public function search() {
		$rs = OrderModel::instance ()->getOrderByWhere ( array ($_POST ['type'] => $_POST ['value'] ) );
		$status = array_flip ( $this->status );
		$status = array_flip ( $this->status );
		foreach ( $rs as $k => $v ) {
			$rs [$k] ['status'] = $status [$v ['status']];
		}
		$this->view ()->assign ( 'totalnum', 1 );
		$this->view ()->assign ( 'news', $rs );
		$this->view ()->display ( 'file:order/olist.tpl' );
	}
	/**
	 * 对接购物车和产品库  创建订单和用户账户信息
	 */
	public function create() {
		//获取用户信息
		if (empty ( $this->_user_global )) {
			$this->redirect ( "请先登录", "/anonymous/login/" );
		}
		
		$user = $this->_user_global;
		
		//获取提交的订单信息 过滤多余字段
		$remark = trim ( $_POST ['remark'] );
		//获取提交的信息 只要产品id和产品数量 
		if (empty ( $_POST ['id'] )) {
			return "购物车是空的";
		}
		
		$data = array ();
		foreach ( $_POST ['id'] as $k => $v ) {
			if (empty ( $_POST ['num'] [$k] )) {
				continue;
			}
			$data [$k] ['id'] = trim ( $v );
			$data [$k] ['num'] = trim ( $_POST ['num'] [$k] );
		}
		
		//判断产品类型
		//判断用户积分或者金额是否足够
		//获取sno号
		$rs = OrderModel::instance ()->getSno ();
		$time = time ();
		//创建订单号
		$sno = $rs ['value'] . date ( 'YmdHis', $time );
		$ginfo = $this->getGoodsInfo ( $data, $sno );
		if ($user ['coupons'] < $ginfo ['couponstotal']) {
			$this->redirect ( "积分不够", "/a/c/1031.html" );
		}
		
		//订单列表  生成唯一sno号 判断库存是否有库存
		$this->addOrder ( $sno, $ginfo ['name'], $user ['uid'], $user ['real_name'], $user ['real_name'], $user ['area'], $user ['mobile_phone'], $ginfo ['num'], $ginfo ['couponstotal'], $ginfo ['moneytotal'], $remark, $this->status ['等待审核'] );
		//订单明细
		$this->addGoods ( $ginfo ['goodslist'] );
		//扣除积分或者金额      
		$this->saveUserCoupons ( $user ['uid'], $ginfo ['couponstotal'], $ginfo ['name'], $this->payment ["支出"], $this->chargeType ["消费"], $sno, $this->status ['等待审核'] );
		
		setcookie ( "jsorder", "", time () - 3600, "/" );
		
		$this->redirect ( "提交成功", "/order/trade/" );
	}
	
	/**
	 * 订单列表
	 * Enter description here ...
	 */
	public function listing() {
		// 默认设置为15条
		if ($_GET ['num'] > 0) {
			$num = trim ( $_GET ['num'] );
		} else {
			$newNum = require 'config/base.php';
			$num = 40;
		}
		if (! empty ( $_REQUEST ['value'] )) {
			$where = array ($_REQUEST ['type'] => trim ( $_REQUEST ['value'] ) );
		} else {
			$where = null;
		}
		// $totalNum = 2000;
		$totalNum = OrderModel::instance ()->getOrderTotalNum ();
		$page = $this->page ( $totalNum ['num'], $num );
		$this->view ()->assign ( 'num', $page );
		$rs = OrderModel::instance ()->orderPage ( $where, $page ['start'], $page ['num'] );
		$status = array_flip ( $this->status );
		
		foreach ( $rs as $k => $v ) {
			$rs [$k] ['status'] = $status [$v ['status']];
		}
		
		$this->view ()->assign ( 'totalnum', $totalNum );
		$this->view ()->assign ( 'news', $rs );
		$this->view ()->assign ( 'status', $this->status );
		$this->view ()->assign ( "cachetype", 1 );
		$this->view ()->display ( 'file:order/olist.tpl' );
	}
	
	/**
	 * 修改订单状态 和积分状态
	 */
	public function setStatus() {
		//设置订单状态  用户只能关闭 管理员审核  
		$user = unserialize ( $_SESSION ['uid'] );
		//获取登录用户信息
		$userinfo = MemberModel::instance ()->getOneMember ( array ('uid' => $user ['uid'] ) );
		//检查当前状态
		$order = OrderModel::instance ()->getOrderByWhere ( array ('orderno' => $_POST ['orderno'] ) );
		$order = $order [0];
		$orderUserinfo = MemberModel::instance ()->getOneMember ( array ('uid' => $order ['uid'] ) );
		if (empty ( $order ))
			return;
		
		//订单为成功或者失败后就不允许修改
		if ($order ['status'] == $this->status ['成功'] || $order ['status'] == $this->status ['失败']) {
			return;
		}
		
		//如果status为关闭 就归还积分
		OrderModel::instance ()->saveStatus ( array ('status' => $_POST ['status'], 'pay_time' => time (), 'action' => $user ['real_name'] ), array ('orderno' => $_POST ['orderno'] ) );
		MemberModel::instance ()->setCouponsStatus ( array ('status' => $_POST ['status'] ), array ('orderno' => $_POST ['orderno'] ) );
		if ($_POST ['status'] == - 1) {
			$coupons = $order ['coupons'] + $orderUserinfo ['coupons'];
			MemberModel::instance ()->setMemberByWhere ( array ('coupons' => $coupons ), array ('uid' => $orderUserinfo ['uid'] ) );
		}
		$this->sendNotice ( self::SUCEESS );
	}
	
	/**
	 * 对接购物车 展示产品信息
	 */
	public function confirm() {
		$cart = $this->parseShopCart ( $_POST ['goodsinfo'] );
		$this->view ()->assign ( "totalcount", $cart ['totalcount'] );
		$this->view ()->assign ( "news", $cart ['goodsinfo'] );
		$this->view ()->display ( "mysql:orderinfo.tpl" );
	
	}
	
	/*获取订单详情*/
	public function i() {
		$goods = OrderModel::instance ()->getGoodsBySNO ( trim ( $_GET ['sno'] ) );
		/*增加算法,代理价优先*/
		$order = OrderModel::instance ()->getOrderByWhere ( array ('orderno' => trim ( $_GET ['sno'] ) ) );
		$order = $order [0];
		$this->view ()->assign ( 'order', $order );
		$this->view ()->assign ( 'goods', $goods );
		$this->view ()->assign ( 'p', $_GET ['p'] );
		/*增加提示信息 是否多人同时操作*/
		$wl = NewsModel::instance ()->getNewsByWhere ( array ('cid' => 1079 ) );
		$this->view ()->assign ( 'wl', $wl );
		$this->view ()->display ( 'file:order/goods.tpl' ); /*产品详情页*/
	}
	
	/**
	 * 
	 * 订单明细 根据用户信息
	 */
	public function trade() {
		if (! isset ( $this->_user_global ['uid'] )) {
			return "请先登录";
		}
		
		$where = array ('uid' => $this->_user_global ['uid'] );
		$totalNum = OrderModel::instance ()->countOrder ( $where );
		$page = $this->page ( $totalNum ['num'], $this->_pageNum );
		$rs = OrderModel::instance ()->commentPage ( $where, $page ['start'], $page ['num'] );
		$this->view ()->assign ( 'totalnum', $totalNum );
		$this->view ()->assign ( 'num', $page );
		$status = array_flip ( $this->status );
		//匹配图片
		foreach ( $rs as $k => $v ) {
			$rs [$k] ['status'] = $status [$v ['status']];
			$rs [$k] ['goodslist'] = OrderModel::instance ()->getGoodsInfo ( $v ['orderno'] );
			$rs [$k] ['goodsnum'] = count ( $rs [$k] ['goodslist'] );
		}
		$this->view ()->assign ( 'userinfo', $this->_user_global );
		$this->view ()->assign ( "order", $rs );
		$this->view ()->display ( "mysql:trade.tpl" );
	}
	
	/**
	 * 获取服务累
	 * Enter description here ...
	 */
	public static function getOrderService() {
		return new OrderService ();
	}

}